<template>
    <div>
        <el-page-header title="首页" @back="gotoRoute('/')" content="评测结果管理">
        </el-page-header>
        <el-divider></el-divider>
        <el-form ref="form" :inline="true" :model="evaForm" label-width="100px" size="mini">
            <el-form-item label="评测年份">
                <el-col :span="10">
                    <el-date-picker type="year" placeholder="选择评测年份" v-model="evaForm.ef_year"></el-date-picker>
                </el-col>
            </el-form-item>
            <el-form-item>
                <el-link type="primary" :href="efYearHref" v-show="evaForm.ef_year" target="_blank">按年份下载</el-link>
            </el-form-item>

        </el-form>
        <el-divider></el-divider>
        <el-table :data="tableData.rows" border class="w-100%">
            <el-table-column type="index" width="50">
            </el-table-column>
            <el-table-column prop="object_group_name" label="单位名称" width="200">
            </el-table-column>
            <el-table-column prop="name" label="名称" width="250">
            </el-table-column>
            <el-table-column prop="ef_year" label="测评年份" width="100">
            </el-table-column>
            <el-table-column prop="is_start" label="状态" width="100">
                <template slot-scope="scope">
                    {{ scope.row.is_finished ? '已结束' : '进行中' }}
                </template>
            </el-table-column>
            <el-table-column prop="start_time" label="开始时间" width="200">
            </el-table-column>
            <el-table-column prop="end_time" label="结束时间" width="200">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="250">
                <template slot-scope="scope">
                    <el-link v-if="scope.row.is_finished" type="primary"
                        :href="'/api/result/project?' + encodeProject(scope.row.id)" target="_blank">下载汇总表</el-link>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background @current-change="handPage" layout="prev, pager, next" :total="tableData.count"
            :page-size="tableData.offset">
        </el-pagination>
    </div>
</template>

<script>
import { BaseApi } from '../../../api/common/base-api';
import { sharedConfig } from '../../shared/BaseCurd';
const api = new BaseApi({ prefix: "/eva-project" });
export default {
    name: "Pager",
    async mounted() {
        this.loadPager({ page: 1 });
    },
    methods: {
        ...sharedConfig.methods,
        async loadPager({ page }) {
            const data = await api.findPager({ page, order: [["is_start"]] });
            this.tableData = { ...data.data };
        },
        encodeProject(id) {
            return "where=" + encodeURIComponent(`{"project_id":"${id}","is_test":${this.is_test}}`)
        }

    },

    data() {
        return {
            evaForm: {
                ef_year: '2024',
            },
            is_test: this.$route.query.is_test || false,
            tableData: {
                count: 0,
                offset: 10,
                page: 1,
                rows: [],
            }
        }
    },
    computed: {
        efYearHref() {
            const param = encodeURIComponent(`{"ef_year":${this.evaForm.ef_year},"is_test":${this.is_test}}`)
            return `/api/result/project?where=${param}`
        }
    }
}
</script>